<?php

namespace app\api\controller\ykjp\information\basisinfo;

use app\common\controller\Api;
use think\Db;

/**
 * 客户信息
 *
 * @icon fa fa-circle-o
 */
class Customerinfo extends Api {

    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = '*';

    /**
     * 查看
     */
    public function index()
    {
        $this->model = new \app\admin\model\ykjp\information\basisinfo\Customerinfo;
        $row = $this->model->where('deletetime',null)->select();
        $this->success('返回成功', ['data' => $row]);
    }

    /**
     * 查看详情
     */
    public function getdetails()
    {
        $id = input('post.id');
        $this->model = new \app\admin\model\ykjp\information\basisinfo\Customerinfo;
        $row = $this->model->where('id',$id)->select();
        $this->success('返回成功', ['data' => $row]);
    }


    /**
     * 删除
     */
    public function del()
    {
        $id = input('post.id');
        $row = \app\admin\model\ykjp\information\basisinfo\Customerinfo::where("id", $id)->delete();
        $this->success('返回成功', ['data' =>'删除成功！']);
    }
    /**
     * 业务员信息
     */
    public function getsalesman() {
        $this->dataLimit=false;
        $result = array("rows" => [], "total" => 0);
        if ($this->request->isAjax()) {
            $this->model = model("admin");
            return parent::selectpage();
            // if($this->request->request("keyValue")){
            //     $id = $this->request->request("keyValue");
            //     $admin=model("admin");
            //     $list=$admin
            //         ->field("id,username")
            //         ->where("firmid",$this->auth->firmid)
            //         ->where("id",$id)
            //         ->select();
            //     return ['total'=>1, 'list'=>$list];
            // }
            // $admin=model("admin");
            // $list=$admin
            //     ->field("id,username")
            //     ->where("firmid",$this->auth->firmid)
            //     ->select();
            // $count=$admin
            //     ->field("id,username")
            //     ->where("firmid",$this->auth->firmid)
            //     ->count();
            // $result=array("rows"=>$list,"total"=>$count);
            // return json($result);
        }
        return json($result);
    }

    /**
     * 添加
     */
    public function add(){
        $name = input('post.name');
        $linkman = input('post.linkman');
        $address = input('post.address');
        $city = input('post.city');
        $remark = input('post.remark');
        $phone = input('post.phone');
        $telephone = input('post.telephone');

        $model = new \app\admin\model\ykjp\information\basisinfo\Customerinfo;
        $number="CT".date("YmdHis",time());
        $time = time();

        $model->insert([
            'name' => $name,
            'contact' => $linkman,
            'address' => $address,
            'city' => $city,
            'description' => $remark,
            'phone' => $phone,
            'code' => $number,
            'createtime' => $time,
            'telephone' => $telephone,
            'level'=>1,
            'salesman_id'=>1,
            'period'=>0,
            'amount'=>'0',
            'status'=>'正常',
            'updatetime' => $time
        ]);

        $this->success('返回成功', ['data' =>'添加成功！']);
    }

}
